package com.jacobson.core.framework.lifecycle

import android.app.Activity
import android.app.Application
import android.os.Bundle
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import com.jacobson.core.App
import com.jacobson.core.base.IActivity
import com.jacobson.core.utils.Density
import com.jacobson.core.widget.logger.Logger
import com.jacobson.core.widget.logger.LoggerFactory
import dagger.Lazy
import org.simple.eventbus.EventBus
import javax.inject.Inject
import javax.inject.Named
import javax.inject.Singleton

/**
 * ================================================
 *
 * @author Wucz on 2018/7/19 9:10
 * ================================================
 */
@Singleton
class ActivityLifecycleCallbacksImpl @Inject constructor() : Application.ActivityLifecycleCallbacks {
    private val logger: Logger = LoggerFactory.getLogger()
    @Inject
    lateinit var app: App

    @Inject
    @field:Named("densitySupport")
    internal lateinit var densitySupport: String

    @Inject
    internal lateinit var fragmentLifecycle: dagger.Lazy<FragmentManager.FragmentLifecycleCallbacks>

    @Inject
    internal lateinit var fragmentLifecycleList: Lazy<MutableList<FragmentManager.FragmentLifecycleCallbacks>>

    override fun onActivityCreated(activity: Activity?, savedInstanceState: Bundle?) {
        logger.v("ActivityLifecycleCallbacks-> onActivityCreated: ${activity?.let { it::class.simpleName }} = $densitySupport")
        activity?.apply {
            densitySupport.takeIf { it.toBoolean() }?.let {
                val annotation = activity.javaClass.getAnnotation(Density.DensityOrientation::class.java)
                if (annotation != null) {
                    Density.setOrientation(activity, annotation.orientation)
                } else {
                    Density.setDefault(this)
                }
            }
            (this as? IActivity)?.run {
                injectBundleParameter()
                if (this.useEventBus()) {
                    EventBus.getDefault().register(this)
                }
                setupActivityComponent(app.provideAppComponent())
            }
            registerFragmentCallbacks(this)
        }
    }


    override fun onActivityPaused(activity: Activity?) {
        logger.v("ActivityLifecycleCallbacks-> onActivityPaused: ${activity?.let { it::class.simpleName }}")
    }

    override fun onActivityResumed(activity: Activity?) {
        logger.v("ActivityLifecycleCallbacks-> onActivityResumed: ${activity?.let { it::class.simpleName }}")
    }

    override fun onActivityStarted(activity: Activity?) {
        logger.v("ActivityLifecycleCallbacks-> onActivityStarted: ${activity?.let { it::class.simpleName }}")
    }

    override fun onActivityDestroyed(activity: Activity?) {
        logger.v("ActivityLifecycleCallbacks-> onActivityDestroyed: ${activity?.let { it::class.simpleName }}")
        (activity as? IActivity)?.run {
            EventBus.getDefault().unregister(this)
        }
    }

    override fun onActivitySaveInstanceState(activity: Activity?, outState: Bundle?) {
        logger.v("ActivityLifecycleCallbacks-> onActivitySaveInstanceState: ${activity?.let { it::class.simpleName }}")
    }

    override fun onActivityStopped(activity: Activity?) {
        logger.v("ActivityLifecycleCallbacks-> onActivityStopped: ${activity?.let { it::class.simpleName }}")
    }


    private fun registerFragmentCallbacks(activity: Activity) {
        (activity as? IActivity)?.apply {
            (activity as? FragmentActivity)?.takeIf { useFragment() }?.apply {
                fragmentLifecycle.get()?.let {
                    supportFragmentManager.registerFragmentLifecycleCallbacks(it, true)
                }
                fragmentLifecycleList.get()?.let { it ->
                    it.forEach { lifecycle ->
                        supportFragmentManager.registerFragmentLifecycleCallbacks(lifecycle, true)
                    }
                }
            }
        }
    }
}